package com.example.clinic.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyl.clinic.common.QueryPageParam;
import com.wyl.clinic.common.Result;
import com.example.clinic.entity.Doctor;
import com.example.clinic.entity.Menu;
import com.example.clinic.entity.User;
import com.example.clinic.mapper.DoctorMapper;
import com.example.clinic.mapper.UserMapper;
import com.example.clinic.service.DoctorService;
import com.example.clinic.service.MenuService;
import com.example.clinic.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangyulong
 * @since 2024-01-11
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    //查询
    @GetMapping("/list")
    public List<User> list(){
        return userService.list();
    }
    //验证
    @GetMapping("/findByAc")
    public Result findByAc(@RequestParam String ac){
        List list = userService.lambdaQuery().eq(User::getAc,ac).list();
        return list.size()>0?Result.suc(list):Result.fail();
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user){


//        System.out.println(user1.getId());
        if (user.getRoleId() == 1) {
            Doctor doctor = new Doctor();
            doctor.setAc(user.getAc());
            doctor.setName(user.getName());
            doctor.setRoleId(user.getRoleId());
            doctorService.save(doctor);
        }
        return userService.save(user)?Result.suc():Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.updateById(user)?Result.suc():Result.fail();
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody User user){
        return userService.updateById(user);
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOrMod(@RequestBody User user){
        return userService.saveOrUpdate(user);
    }
    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user){
//        List list = userService.lambdaQuery().eq(User::getAc,user.getAc()).eq(User::getPassword,user.getPassword()).list();
//        return list.size()>0?Result.suc(list.get(0)):Result.fail();
        List list = userService.lambdaQuery().eq(User::getAc,user.getAc()).eq(User::getPassword,user.getPassword()).list();

        if(list.size()>0){
            User user1 = (User)list.get(0);
            List menuList = menuService.lambdaQuery().like(Menu::getMenuright,user1.getRoleId()).list();
            HashMap res = new HashMap();
            res.put("user",user1);
            res.put("menu",menuList);
            return Result.suc(res);
        }
        return Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        QueryWrapper<User>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user1 = userMapper.selectOne(queryWrapper);
        System.out.println(user1);
        QueryWrapper<Doctor>queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ac",user1.getAc());
        int result = doctorMapper.delete(queryWrapper1);
        System.out.println(result);
//        QueryWrapper<Doctor>queryWrapper1 = new QueryWrapper<>();
//        String ac = user1.getAc();
//        queryWrapper.eq("ac",ac);
//        Doctor doctor = doctorMapper.selectOne(queryWrapper1);
//        System.out.println(doctor.getId());
//
////        doctorService.removeById(doctor.getId());
        return userService.removeById(id)?Result.suc():Result.fail();
    }
    //查询
    @PostMapping("/listP")
    public Result listP(@RequestBody User user){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(user.getName())) {
            lambdaQueryWrapper.like(User::getName,user.getName());
        }
        return Result.suc(userService.list(lambdaQueryWrapper));
    }

    @PostMapping("/listPage")
    public List<User> listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");


//        HashMap param = query.getParam();
//        System.out.println("name==="+(String)param.get("name"));
//        System.out.println("no==="+(String)param.get("no"));

//        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
//        lambdaQueryWrapper.like(User::getName,user.getName());
//        return userService.list(lambdaQueryWrapper);
        Page<User> page = new Page(1,2);
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(User::getName,name);

        IPage result = userService.page(page,lambdaQueryWrapper);

        System.out.println("总记录"+result.getTotal());


        return result.getRecords();
    }


    @PostMapping("/listPageC")
    public Result listPageC(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String roleId = (String)param.get("roleId");

        Page<User> page = new Page(1,2);
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.like(User::getName,name);
        }
        if(StringUtils.isNotBlank(roleId)){
            lambdaQueryWrapper.eq(User::getRoleId,roleId);
        }


        IPage result = userService.page(page,lambdaQueryWrapper); //注意这里

        System.out.println("总记录"+result.getTotal());


        return Result.suc(result.getRecords(),result.getTotal());
    }

}
